哈囉 ~ 在前一篇文章中,我們討論了物件導向的基本概念,現在就讓我們使用C#語言來實現物件導向編程的概念吧。
// 類別
public class Car
{
    // 屬性
    public string Make;
    public int Year;
    // 方法
    public void Start()
    {
        Console.WriteLine("創建於"+Year+"年的"+Make+"汽車已啟動!");
    }
    public void Stop()
    {
        Console.WriteLine("汽車已停止!");
    }
}
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Year = 2020;
myCar.Start();
myCar.Stop();
創建於2020年的Toyota汽車已啟動!
汽車已停止!
public class Car
{
    public string Make;
    public string Model;
    public int Year;
    public void Start()
    {
        Console.WriteLine("汽車已啟動!");
    }
}
namespace ithome_test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Car myCar = new Car();
            myCar.Make = "Toyota";
            myCar.Model = "Camry";
            myCar.Year = 2020;
            myCar.Start();
        }
    }
}
public class Car
{
    private string Make;
    private string Model;
    private int Year;
    public void Start()
    {
        Console.WriteLine("汽車已啟動!");
    }
    public void SetMake(string make)
    {
        Make = make;
    }
    public string GetMake()
    {
        return Make;
    }
}
namespace ithome_test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Car myCar = new Car();
            // 這裡無法直接訪問 Make 屬性,因為它是 private 的
            // myCar.Make = "Toyota";  // 這會產生編譯錯誤
            // 但我們可以使用公開的 SetMake 和 GetMake 方法訪問 Make 屬性
            myCar.SetMake("Toyota");
            Console.WriteLine($"汽車製造商:{myCar.GetMake()}");
            myCar.Start();
        }
    }
}
using System;
// 類別
public class Vehicle
{
    protected string Make;
    protected string Model;
    public Vehicle(string make, string model)
    {
        Make = make;
        Model = model;
    }
    public void Start()
    {
        Console.WriteLine($"{Make} {Model} 已啟動!");
    }
}
public class Car : Vehicle
{
    public Car(string make, string model) : base(make, model)
    {
    }
}
namespace ithome_test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Car myCar = new Car("Toyota", "Camry");
            myCar.Start();  // 可以訪問基類的 protected 屬性和方法
        }
    }
}
Toyota Camry 已啟動!
public class Car
{
    // 屬性
    public string Make;
    public string Model;
    public int Year;
    // 方法
    public void Start()
    {
        Console.WriteLine("汽車已啟動!");
    }
    public void Stop()
    {
        Console.WriteLine("汽車已停止!");
    }
}
public class ElectricCar : Car
{
    public int BatteryCapacity;
    public void Charge()
    {
        Console.WriteLine("電動汽車正在充電!");
    }
}
namespace ithome_test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ElectricCar myElectricCar = new ElectricCar();
            myElectricCar.Make = "Tesla";
            myElectricCar.Model = "Model S";
            myElectricCar.Year = 2022;
            myElectricCar.BatteryCapacity = 75;
            myElectricCar.Start();
            myElectricCar.Charge();
        }
    }
}
using System;
// 建立一個基底類別 Animal
public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("某種動物正在發出聲音");
    }
}
// 建立一個派生類別 Dog,繼承自 Animal
public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("狗在汪汪叫!");
    }
}
// 建立一個派生類別 Cat,繼承自 Animal
public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("貓在喵喵叫!");
    }
}
class Program
{
    static void Main(string[] args)
    {
        // 建立 Animal、Dog 和 Cat 物件
        Animal myAnimal = new Animal();
        Animal myDog = new Dog();
        Animal myCat = new Cat();
        // 呼叫 MakeSound 方法
        myAnimal.MakeSound();  // 某種動物正在發出聲音
        myDog.MakeSound();     // 狗在汪汪叫!
        myCat.MakeSound();     // 貓在喵喵叫!
    }
}
某種動物正在發出聲音
狗在汪汪叫!
貓在喵喵叫!
今天介紹了物件導向的實作,那就到這裡啦 ~ 掰掰 !
https://learn.microsoft.com/zh-tw/dotnet/csharp/fundamentals/tutorials/oop